package com.moon.game.command.guest.lair;

import java.util.LinkedList;
import java.util.List;

import com.moon.game.command.BaseCmd;
import com.moon.game.command.RequestSession;
import com.moon.game.constants.Constants;
import com.moon.game.constants.ErrorCode;
import com.moon.game.exception.GException;
import com.moon.game.service.LairService;
import com.moon.game.service.PlayerService;
import com.moon.game.util.GDateUtils;
import com.moon.game.vos.ErrorMsg;
import com.moon.game.vos.PlayerVo;
import com.moon.game.vos.PlayerVo.Tower;
import com.moon.game.vos.RentLairBean;

/**
 * @description 续期
 * @author Ranger 作者 E-mail:haozi000005@163.com
 * @version 创建时间：2012-8-2 下午4:54:34
 */
public class RenewalTheLairCmd extends BaseCmd {

	@Override
	protected void runOnce(RequestSession req) throws GException {
		switch (req.getStatus()) {
			case STATU_INIT:
				doInit(req);
				break;
			case STATU_CHECK:
				doCheckSelf(req);
				break;
			case STATU_RETURN:
				doResponse(req);
				break;
			default:
				break;
		}

	}

	@Override
	protected void doResponse(RequestSession req) throws GException {
		String[] ps = req.getClientRequest().getPs();
		if (ps == null) {
			doResponsePsError(req);
			return;
		}
		PlayerVo player = req.getPlayer();
		List<Tower> towers = player.getTowerList();
		if (towers == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "检测不到你的孵化塔，不能出租！"));
			return;
		}
		Integer towerId = Integer.parseInt(ps[0]);
		if (towerId == null) {
			doResponsePsError(req);
			return;
		}
		Tower theTower = LairService.findLair(towers, towerId);
		//没有指定孵化塔
		if (theTower == null) {
			doResponseErrorMsg(req, ErrorMsg.build(ErrorCode.ERR_NO_TOWER, "你当前没有指定的孵化塔！"));
			return;
		}
		req.setVO("tower_id", towerId);
		Integer ack = Integer.parseInt(ps[1]);
		if (ack == null) {
			doResponsePsError(req);
			return;
		}
		LinkedList<RentLairBean> rantingLairs = LairService.getRentingLairs();
		if (rantingLairs == null || rantingLairs.size() == 0) {
			theTower.setRentTime(0);
			theTower.setStartRentTime(0);
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_SYS, "租赁处没有看到在出租的孵化塔！"));
			return;
		}
		
		
		RentLairBean bean = LairService.findRendLairBean(rantingLairs, player, towerId);
		if (bean == null) {
			theTower.setRentTime(0);
			theTower.setStartRentTime(0);
			doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_SYS, "租赁处找到你指定的孵化塔！"));
			return;
		}
		int price = Constants.BASE_RANT_AGAIN_PRICE * (2 << bean.getRentingTimes());
		req.setVO("price", price);
		if (ack == 1) {
			//确认
			StringBuffer info = new StringBuffer();
			boolean minusStatu = PlayerService.minusGold(player.getSid(), player.getId(), price);
			if (minusStatu) {
				theTower.setRentTime(theTower.getRentTime() + Constants.ONCE_RENEWAL_ADDED_TIME);
				bean.addRentingTimes();
				bean.setTower(theTower);
				LairService.UpdataTheRendBean(rantingLairs, bean);
				PlayerService.doUpdatePlayer(player.getSid(), player.getId(), player);
				info.append("续期成功，该孵化塔将在" + GDateUtils.formatFromLong((theTower.getStartRentTime() + theTower.getRentTime()) * 1000l, "yyyy年MM月dd日 HH时mm分ss") + "以后过期，金币扣除" + price + "！");
			}else {
				doResponseErrorMsg(req,ErrorMsg.build(ErrorCode.ERR_NO_GOLD, "你的金币不足" + price + "，不能进行续期！"));
				return;
			}
			req.setVO(TAG_INFO, info);
		}else {
			//待确认
			req.setVO(TAG_ACK, true);
		}
		response(req, req.getClientResponse());
	}

}
